<?php

namespace app\admin\controller;

use app\admin\services\ArticleTypeService;
use think\App;

class ArticleTypeController extends Common implements ICurd
{
    public function index()
    {
        // TODO: Implement index() method.
        if (request()->isPost()) {
            $requestData = input('post.');
            $param['page'] = $requestData['page'] ?: 1;
            $param['pageSize'] = $requestData['limit'] ?: 10;
            $param['key'] = trim($requestData['key']) ?: "";
            // 需要查询的字段
            $field = "
                id,
                title,
                status
            ";
            $list = $this->getService()->list($param,$field);
            return $this->jsonList($list['data'], $list['total']);
        }
        return $this->fetch("article_type/index");
    }

    public function create()
    {
        // TODO: Implement create() method.
        if (request()->isPost()) {
            $param = $this->mapping();
            $res = $this->getService()->save($param);
            if ($res) {
                return $this->jsonSuccess("index");
            }
            return $this->jsonError();
        }
        return $this->fetch("article_type/create");
    }

    public function update($id = 0)
    {
        // TODO: Implement update() method.
        if (request()->isPost()) {
            $param = $this->mapping();
            $res = $this->getService()->save($param,$id);
            if ($res) {
                return $this->jsonSuccess("index");
            }
            return $this->jsonError();
        } else {
            $info = $this->getService()->getInfoById($id);
            $this->assign([
                'info' => json_encode($info, true),
            ]);
            return $this->fetch("article_type/update");
        }
        return $this->jsonError();
    }

    public function remove()
    {
        // TODO: Implement remove() method.
        if (request()->isPost()) {
            $data = input('post.');
            $id = (int) $data['id'];
            $res = $this->getService()->save(['is_delete'=>2],$id);
            if ($res) {
                return $this->jsonSuccess("index");
            }
            return $this->jsonError();
        }
        return $this->jsonError();
    }

    public function detail()
    {
        // TODO: Implement detail() method.
    }

    public function setStatus()
    {
        // TODO: Implement setStatus() method.
        if (request()->isPost()) {
            $id = input('post.id', 0, 'int');
            $status = input('post.status', 0, 'int');
            $res = $this->getService()->save([
                'status' => $status
            ],$id);
            if ($res) {
                return $this->jsonSuccess('index');
            }
            return $this->jsonError();
        }
        return $this->jsonError();
    }

    private function getService()
    {
        return ArticleTypeService::makeInstance();
    }

    private function mapping()
    {
        $data = input('post.');
        $param = [
            'title' => trim($data['title']) ?: "",
            'status' => (int)$data['status'] ?: 1 ,
            'sort' => (int)$data['sort'] ?: 0
        ];
        return $param;
    }

}